package cutetd.vo;

import java.util.ArrayList;
import java.util.List;
import cutetd.db.GaeDbBase;

public class PageNavi {
	private int max = 2;// 每页显示的最大数据条目
	private int curPage = 1;
	private int maxPage = 5; // 每页显示的最大页面数量
	private List<PageNavi> pageList = new ArrayList<PageNavi>();

	private Class<?> c;

	GaeDbBase gae = new GaeDbBase();

	/**
	 * @param c
	 *            分页查询的类
	
	 * @param max
	 * 			每页列表的最多数目
	 * @param maxPage
	 * 			每页最多分页数
	 * 
	 */
	public PageNavi(Class<?> c, int max, int maxPage) {
		this.c = c;
		this.max = max;
		this.maxPage = maxPage;
	}
	
	/**根据当前页与查询条件得到分页列表
	 * @param curPage
	 * @return
	 */
	public List<Page> getPageListByCondition(int curPage, String where) {
		
		return page(curPage, getTotalPagesByCondition(where));
	}
	/**根据当前页得到分页列表
	 * @param curPage
	 * @return
	 */
	public List<Page> getPageList(int curPage) {
		return page(curPage, getTotalPages());
	}
	
	public List<Page> page(int curPage, int totalPage) {
		List<Page> pageList = new ArrayList<Page>();

		if (curPage == 1) {
			pageList.add(getLabelPage(false, curPage - 1, "上一页"));
		} else {
			pageList.add(getLabelPage(true, curPage - 1, "上一页"));
		}
		// 当前页数<=maxPage/2时
		if (curPage <= maxPage / 2) {
			List<Page> list = getList(1, curPage, curPage);
			pageList.addAll(list);

			if (pageList.size() < maxPage) {
				if (totalPage > maxPage) {
					list = getList(curPage + 1, maxPage, curPage);
					pageList.addAll(list);
				} else if (curPage < totalPage && totalPage <= maxPage){
					list = getList(curPage + 1, totalPage, curPage);
					pageList.addAll(list);
				}			
			}
		}

		// 当前页面<= maxPage + 1时，后面再多显示3页
		else if (curPage <= maxPage + 1) {
			List<Page> list = getList(1, curPage, curPage);
			pageList.addAll(list);
		
			if (curPage + 3 <= totalPage) {
				pageList.addAll(getList(curPage + 1, curPage + 3,
						curPage));
			} else if (curPage + 3 > totalPage) {
				pageList.addAll(getList(curPage + 1, totalPage, curPage));
			}
		}
		// 当前页数>=maxPage + 2时,且后面再多显示3页
		else if (curPage + 2 > maxPage) {
			List<Page> list = getList(1, 2, curPage);
			
			pageList.addAll(list);
			pageList.add(getLabelPage(false, curPage, "..."));
			pageList.addAll(getList(curPage - 3, curPage, curPage));
			if (curPage + 3 <= totalPage) {
				pageList.addAll(getList(curPage + 1, curPage + 3,
						curPage));
			} else if (curPage + 3 > totalPage) {
				pageList.addAll(getList(curPage + 1, totalPage, curPage));
			}
		}
		// 如果后续还有更多页面增加...
		if (curPage + 3 < totalPage && curPage >= maxPage + 2) {
			pageList.add(getLabelPage(false, curPage, "..."));
		}

		// 如果是最后一页
		if (curPage >= totalPage) {
			
			pageList.add(getLabelPage(false, curPage + 1, "下一页"));
		} else {
			pageList.add(getLabelPage(true, curPage + 1, "下一页"));
		}
		//pageList.add(getLabelPage(false, curPage, "共" + String.valueOf(totalPage) + "页"));
		return pageList;
	}

	public List<?> getObjectList(int curPage, String condition, String order) {
		return (List<?>) gae.getSubList(c, (curPage - 1) * max, curPage * max, condition, order);
	}

	/**
	 * @param start
	 *            开始页数
	 * @param end
	 *            结束页数
	 * @param cur
	 *            当前页数(无连接)
	 * @return
	 */
	public List<Page> getList(int start, int end, int cur) {
		List<Page> list = new ArrayList<Page>();

		for (int i = start; i <= end; ++i) {
			Page page = new Page();
			page.setDisplayName(String.valueOf(i));

			if (i != cur) {
				page.setBoolDisplay(true);
			} else {
				page.setBoolDisplay(false);
			}
			page.setUrl("curPage=" + String.valueOf(i));
			list.add(page);
		}

		return list;
	}

	/**
	 * 得到带自定义名字的page
	 * 
	 * @param display
	 *            链接是否显示
	 * @param curPage
	 *            当前页数值
	 * @param name
	 *            显示的名称如：上一页， 下一页， ...
	 * @return
	 */
	public Page getLabelPage(boolean display, int curPage, String name) {
		Page page = new Page();
		page.setDisplayName(name);
		if (display) {
			page.setBoolDisplay(true);
			page.setUrl("curPage=" + String.valueOf(curPage));
		} else {
			page.setBoolDisplay(false);
		}

		return page;
	}

	public int getTotalNum() {
		return gae.getTotalNum(c);
	}

	public int getTotalNumByCondition(String where) {
		return gae.getTotalNumByCondition(c, where);
	}
	
	public int getTotalPages() {
		return (int) Math.ceil(((double) getTotalNum() / (double) max));
	}
	
	public int getTotalPagesByCondition(String where) {
		return (int) Math.ceil(((double) getTotalNumByCondition(where) / (double) max));
	}

	public List<PageNavi> getPageList() {
		return pageList;
	}

	public void setPageList(List<PageNavi> pageList) {
		this.pageList = pageList;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
}
